package com.liukai.bishe.ui.activity.loginAndRegister;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;


import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.model.doman.user.UserMessage;
import com.liukai.bishe.presenter.loginAndRegisterPresenter.IRegisterPresenter;
import com.liukai.bishe.presenter.loginAndRegisterPresenter.impl.RegisterPresenterImpl;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IRegisterCallBack;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,IRegisterCallBack {

    @BindView(R.id.btnBack)
    public ImageButton btnBack;
    @BindView(R.id.tvUser)
    public EditText tvUser;
    @BindView(R.id.tvPass)
    public EditText tvPass;
    @BindView(R.id.tvConfirm)
    public EditText tvConfirm;
    @BindView(R.id.btnRegister)
    public AppCompatButton btnRegister;
    private Context context = RegisterActivity.this;

    private Unbinder mBind;
    private IRegisterPresenter mRegisterPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mBind = ButterKnife.bind(this);
        initListener();

    }

    /**
     * 设置监听
     */
    private void initListener(){
        btnBack.setOnClickListener(this);
        tvUser.setOnClickListener(this);
        tvPass.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    /**
     * 点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            // 返回
            case R.id.btnBack :
                finish();
                break;

                // 注册
            case R.id.btnRegister :

                validateRegister();

                break;

            }

    }

    /**
     * 判断是否可以发起注册网络请求
     */
    private void validateRegister() {
        String username = tvUser.getText().toString().trim();
        String password = tvPass.getText().toString().trim();
        String confirm = tvConfirm.getText().toString().trim();
        ToastUtil.log(this,"username",username);
        ToastUtil.log(this,"password",password);
        ToastUtil.log(this,"confirm",confirm);
        tvConfirm.setText("");
        tvPass.setText("");
        tvUser.setText("");
        if (username != null && password != null && confirm != null) {
            if(password.equals(confirm)){
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                register(user);
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind = null;
        }
        if (mRegisterPresenter != null) {
            mRegisterPresenter = null;
        }
    }

    /**
     * 注册
     * @param user
     */
    private void register(User user){

        mRegisterPresenter = new RegisterPresenterImpl();
        mRegisterPresenter.registerCallback(this);
        mRegisterPresenter.register(user);


    }

    /**
     * 注册的返回结果
     * @param object
     */
    @Override
    public void result(Object object) {

        UserMessage userMessage = (UserMessage)object;
        ToastUtil.log(context,"userMessage",userMessage.toString());
        String code = userMessage.getCode(); // 获取登录的返回信息
        if(code.equals("2001")){
            ToastUtil.show(context,"注册成功");
            finish();
        }else if (code.equals("2001")){
            ToastUtil.show(context,"注册失败");
        }

    }
}
